import { defineStore } from 'pinia';

import { ProjectConfig } from '/#/config';
import { CacheTypeEnum } from '/@/enums/cacheEnum';
import { MenuSetting } from '/#/menu/menuSetting';
import { cookies } from '../../utils/cookie/index';
import { store } from '/@/store';

interface appSettingState {
  projectConfig: Partial<ProjectConfig>;
}

export const useAppSettingStore = defineStore({
  id: 'appSetting',
  state: (): appSettingState => ({
    projectConfig: {
      menuSetting: {
        collapsed: true,
      },
      permissionCacheType: CacheTypeEnum.LOCAL,
    },
  }),
  getters: {
    getProjectSetting(): Partial<ProjectConfig> {
      return this.projectConfig || ({} as ProjectConfig);
    },
    getMenuSetting(): MenuSetting | undefined {
      return this.projectConfig?.menuSetting;
    },
    getAppLogoPath(): string {
      return cookies.get('appLogoPath') || ('/src/assets/images/hw_logo.png' as string);
    },
    getTenantName(): string {
      return cookies.get('tenantName') || ('SaaS DreamWorks' as string);
    },
  },
  actions: {
    setProjectConfig(config: Partial<ProjectConfig>): void {
      this.projectConfig = config;
    },

    setMenuSetting(set: MenuSetting): void {
      this.projectConfig.menuSetting = set;
    },
    setAppLogoPath(path: string): void {
      cookies.set('appLogoPath', path, { expires: 7 });
    },
    setTenantName(tenantName: string): void {
      cookies.set('tenantName', tenantName, { expires: 7 });
    },
  },
});

/**
 * @description appsetting store : stuff about projectConfigs and settings
 */
export function useAppSettingStoreWithOut() {
  return useAppSettingStore(store);
}
